package com.ict.ais.realTime.service.api.impl;


import org.springframework.stereotype.Service;

import com.ict.ais.realTime.facade.api.IImportantTargetService;
import com.ict.ais.realTime.facade.dto.ResultObj;
import com.ict.ais.realTime.facade.model.AppointTarget;
import com.ict.ais.realTime.facade.model.InterestTarget;
import com.ict.ais.realTime.facade.model.TargetRule;
import com.ict.ais.realTime.service.dao.IImportantTargetDao;

import mqutils.MQTopicPublish;
import mqutils.ProperUtilsMQ;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/12/7.
 */
@Service("importantTargetService")
public class ImportantTargetService implements IImportantTargetService{
    @Resource
    IImportantTargetDao iImportantTarget;

    public boolean imTargetAdd(int shipId){
        try{
            team.iOceanPlus.PB.Config.PBConfig.Builder imtatgetPB=team.iOceanPlus.PB.Config.PBConfig.newBuilder();

            team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.Builder targetsOfInterestPB=team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.newBuilder();

            targetsOfInterestPB.setTargetID(shipId);

            targetsOfInterestPB.setEnumConfigType(team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);

            imtatgetPB.addListConfigTargetsOfInterest(targetsOfInterestPB);
            ProperUtilsMQ properUtilsMQ=new ProperUtilsMQ("xgs","Configuration.TargetsOfInterest","");

            MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtilsMQ.getHost(), properUtilsMQ.getUser(), properUtilsMQ.getPassWord(),
                    properUtilsMQ.getExchange());

            if (mqTopicPublish.BaseConnection(properUtilsMQ.getPort()) != 0) {
                System.out.println("连接RabbiqMQ失败");
                return false;
            }

            //PB打包成byte[]
            team.iOceanPlus.PB.Config.PBConfig bui = imtatgetPB.build();
            byte[] result = bui.toByteArray();

            //调用RabbitMQ发布到指定的队列
            if (!mqTopicPublish.Publish(properUtilsMQ.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            System.out.println("操作失败" + e);
            e.printStackTrace();
            return false;
        }
    }

    public List<AppointTarget> imTargetGet(){
        try{
            return iImportantTarget.imTargetGet();
        }catch (Exception e){
            System.out.println("数据库操作失败"+e);
            return null;
        }
    }

    /**
     * 根据重点目标批号删除重点目标规则
     * @param shipId
     * @return
     */
    public boolean imTargetDelete(int shipId){
        try{
            int recordId=iImportantTarget.findRecordtoDelete(shipId);
            if (deleterule(recordId)){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            System.out.println("数据库操作失败"+e);
            return false;
        }
    }

    /**
     * 获取重点目标规则列表
     * @return
     */
    public ResultObj getTargetRuleList(){
        ResultObj resultObj=new ResultObj();
        try{
            List<TargetRule> targetRuleList=iImportantTarget.getTargetRuleList();
            if(targetRuleList.size()<1){
                resultObj.setState(0);
            }else {
                resultObj.setState(1);
                resultObj.setObj(targetRuleList);
            }
            return resultObj;
        }catch (Exception e){
            resultObj.setState(-1);
            System.out.println("数据库操作失败："+e);
            return resultObj;
        }
    }

    /**
     * 获取重点目标列表
     * @return
     */
    public ResultObj getInterestTarget(){
        ResultObj resultObj=new ResultObj();
        try{
            List<InterestTarget> targetRuleList=iImportantTarget.getInterestTarget();
            if(targetRuleList.size()<1){
                resultObj.setState(0);
            }else {
                resultObj.setState(1);
                resultObj.setObj(targetRuleList);
            }
            return resultObj;
        }catch (Exception e){
            resultObj.setState(-1);
            System.out.println("数据库操作失败："+e);
            return resultObj;
        }
    }

    /**
     * 重点目标规则设置添加
     * @return
     */
    public boolean setrules(String ship_name,int mmsi,int imo_number,String call_sign){
        try{
            team.iOceanPlus.PB.Config.PBConfig.Builder imtatgetPB=team.iOceanPlus.PB.Config.PBConfig.newBuilder();

            team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.Builder targetsOfInterestPB=team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.newBuilder();

            if(!ship_name.equals("N")){
                targetsOfInterestPB.setShipName(ship_name);
            }

            if(mmsi!=0){
                targetsOfInterestPB.setMMSI(mmsi);
            }

            if (imo_number!=0){
                targetsOfInterestPB.setIMONum(imo_number);
            }

            if(!call_sign.equals("N")){
                targetsOfInterestPB.setCallSign(call_sign);
            }

            targetsOfInterestPB.setEnumConfigType(team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Add);

            imtatgetPB.addListConfigTargetsOfInterest(targetsOfInterestPB);
            ProperUtilsMQ properUtilsMQ=new ProperUtilsMQ("xgs","Configuration.TargetsOfInterest","");

            MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtilsMQ.getHost(), properUtilsMQ.getUser(), properUtilsMQ.getPassWord(),
                    properUtilsMQ.getExchange());

            if (mqTopicPublish.BaseConnection(properUtilsMQ.getPort()) != 0) {
                System.out.println("连接RabbiqMQ失败");
                return false;
            }

            //PB打包成byte[]
            team.iOceanPlus.PB.Config.PBConfig bui = imtatgetPB.build();
            byte[] result = bui.toByteArray();

            //调用RabbitMQ发布到指定的队列
            if (!mqTopicPublish.Publish(properUtilsMQ.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            System.out.println("操作失败" + e);
            return false;
        }
    }

    /**
     * 重点目标规则设置删除
     * @return
     */
    public boolean deleterule(int id){
        try{
            team.iOceanPlus.PB.Config.PBConfig.Builder imtatgetPB=team.iOceanPlus.PB.Config.PBConfig.newBuilder();

            team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.Builder targetsOfInterestPB=team.iOceanPlus.PB.Config.PBConfigTargetsOfInterest.newBuilder();

            targetsOfInterestPB.setEnumConfigType(team.iOceanPlus.PB.Config.PB_Enum_ConfigType.EV_ConfigType_Delete);
            targetsOfInterestPB.setRecordID(id);

            imtatgetPB.addListConfigTargetsOfInterest(targetsOfInterestPB);
            ProperUtilsMQ properUtilsMQ=new ProperUtilsMQ("xgs","Configuration.TargetsOfInterest","");

            MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtilsMQ.getHost(), properUtilsMQ.getUser(), properUtilsMQ.getPassWord(),
                    properUtilsMQ.getExchange());

            if (mqTopicPublish.BaseConnection(properUtilsMQ.getPort()) != 0) {
                System.out.println("连接RabbiqMQ失败");
                return false;
            }

            //PB打包成byte[]
            team.iOceanPlus.PB.Config.PBConfig bui = imtatgetPB.build();
            byte[] result = bui.toByteArray();

            //调用RabbitMQ发布到指定的队列
            if (!mqTopicPublish.Publish(properUtilsMQ.getRoutingKey(), result)) {
                System.out.println("发送RabbitMQ消息失败");
                return false;
            }
            return true;
        } catch (Exception e) {
            System.out.println("操作失败" + e);
            return false;
        }
    }


//    public List imTargetCare(int shipId){
//        try{
//            return null;
//        }catch (Exception e){
//            System.out.println("数据库操作失败"+e);
//            return null;
//        }
//    }
}